package xyz.wadewhy.basicstest.designPatterns.AbstractFactoryModel;
import org.apache.commons.lang3.StringUtils;
import java.io.*;

/**
 * @ClassName Test
 * @Description 业务用途
 * @Author zhongzihao
 * @Date 2021/12/19 7:41 下午
 * @Version 1.0
 **/
public class Test {
    private static FurnitureFactory furnitureFactory;
    private static final String CHINESE = "chinese";
    private static final String ENGLISH = "english";

    public FurnitureFactory initFurnitureFactory(String type) {
        if (StringUtils.equals(type, CHINESE)) {
            return new ChineseFurniture();
        }
        if (StringUtils.equals(type, ENGLISH)) {
            return new EnglishFurniture();
        }
        return null;
    }
    public void inputMethod() {
        System.out.println("请输入：chinese或english");
        BufferedReader bufr =
                new BufferedReader(new InputStreamReader(System.in));
        /**
         * 输入顺序 键盘-->内存  键盘是字节流，如果要取得字符流就需要转换
         */
        BufferedWriter bufw =
                new BufferedWriter(new OutputStreamWriter(System.out));
        /**
         * 输出顺序 内存-->屏幕  屏幕输出的是字节流，所以将内存中字符流转换成字节流，然后在转化成字符流输出
         */
        /**
         * 上面两个转换是否重复了，我们输入直接读取字节流，然后让字节流直接放进OutputStreamWriter?
         */

        String line;

        try {
            while ((line = bufr.readLine()) != null) {

                FurnitureFactory factory = initFurnitureFactory(line);
                if (factory == null) {
                    System.err.println("类型错误");
                    break;
                }
                FurnitureUtil furnitureUtil = new FurnitureUtil(factory);
                furnitureUtil.begin();
                bufw.newLine();
                bufw.flush();
                //bufw.newLine(); 注意如果这条语句写在此处，换行是不正确的，bufw的数据要在“flush”后才会显示
                //空行会显示在 bufw.write(line)的前面。儿不是我们想要的bufw.newline()后面
            }
        } catch (IOException e) {
            System.out.println("读写错误！");
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Test t = new Test();
        t.inputMethod();
    }
}
